package masterjava.common.server.gwt.rpc;

import masterjava.common.logging.ServerLogger;
import masterjava.common.shared.rpc.Request;
import masterjava.common.shared.rpc.Response;
import masterjava.common.util.Util;

public abstract class RpcHandlerImpl<RQ extends Request, RS extends Response> implements RpcHandler<RQ, RS> {
    protected ServerLogger logger = ServerLogger.get(getClass());

    protected abstract void handler(RQ request, RS response);

    public RS handlerRequest(RQ request) throws Exception {
        RS response = getResponse();
        if (validateData(request, response)) {
            handler(request, response);
        }
        return response;
    }

    protected boolean validateData(RQ request, RS response) {
        return true;
    }

    protected RS getResponse() throws IllegalAccessException, InstantiationException {
        Class<RS> clazz = Util.getGenericParameter(this, 1);
        return clazz.newInstance();
    }
}
